package com.yintong.presenter;

import android.text.TextUtils;

import com.lyb.app.App;
import com.lyb.base.BasePresenter;
import com.lyb.events.FragmentRefreshEvent;
import com.lyb.events.UIBaseEvent;
import com.lyb.http.HttpManager;
import com.lyb.http.HttpSubscriber;
import com.lyb.ui.my.bean.UserInfoBean;
import com.lyb.util.LogUtils;
import com.lyb.util.ToastUtil;
import com.yintong.activity.BindBankActivity;
import com.yintong.bean.BindBankBean;
import com.yintong.bean.GetUserInfoBean;
import com.yintong.contract.BindBankContract;
import com.yintong.pay.utils.BaseHelper;
import com.yintong.pay.utils.PayOrder;
import com.yintong.secure.demo.env.EnvConstants;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by keepking on 2017/8/2.
 */

public class BindBankPresenter extends BasePresenter<BindBankContract.View> implements BindBankContract.Presenter {
    @Override
    public void bindCard(String cardNo,GetUserInfoBean bean) {
        final PayOrder order = new PayOrder();
        if(App.getConfig().getUserInfo() != null){
            UserInfoBean user = App.getConfig().getUserInfo();
            order.setSign_type(PayOrder.SIGN_TYPE_RSA);
            order.setUser_id(user.getUid()+""); //用户ID
            order.setId_no(bean.getCardNo()); //身份证
            order.setAcct_name(bean.getUserName());//用户名
            order.setCard_no(cardNo); //银行卡号
            order.setOid_partner(EnvConstants.PARTNER); //商户号
            String content = BaseHelper.sortParamForSignCard(order);
            toSubscribe(HttpManager.getApi().getSignRSA2(bean.getUserName(),cardNo,bean.getCardNo(),user.getUid()+""), new HttpSubscriber<Object>() {
                @Override
                protected void _onStart() {
                    mView.showLoading("");
                }

                @Override
                protected void _onNext(Object bean) {
                    mView.showErrorMsg("绑卡成功","dialog");
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorMsg(message,null);
                }


                @Override
                protected void _onError(String message, int code) {
                    mView.showErrorMsg(message,code+"");
                }

                @Override
                protected void _onCompleted() {
                    mView.stopLoading();
                }
            });
        }
    }


    @Override
    public void getUserInfo() {
        String userId = "";
        if(App.getConfig().getUserInfo() != null){
            userId = App.getConfig().getUserInfo().getUid()+"";
        }
        if(TextUtils.isEmpty(userId)){
            ToastUtil.show("无效的用户id");
            return;
        }
        toSubscribe(HttpManager.getApi().getUserInfo(userId), new HttpSubscriber<GetUserInfoBean>() {
            @Override
            protected void _onStart() {
                mView.showLoading("");
            }

            @Override
            protected void _onNext(GetUserInfoBean bean) {
                mView.getUserInfoSuccess(bean);
            }

            @Override
            protected void _onError(String message) {
                mView.showErrorMsg(message,"finish");
            }

            @Override
            protected void _onCompleted() {
                mView.stopLoading();
            }
        });
    }

    @Override
    public void uploadBindState(String stateContent) {
        String userId = "";
        SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String reportTime = formater.format(Calendar.getInstance().getTime());
        if(App.getConfig().getUserInfo() != null){
            userId = App.getConfig().getUserInfo().getUid()+"";
        }

        toSubscribe(HttpManager.getApi().uploadBindState(userId,reportTime,stateContent), new HttpSubscriber<GetUserInfoBean>() {
            @Override
            protected void _onStart() {

            }

            @Override
            protected void _onNext(GetUserInfoBean bean) {
            }

            @Override
            protected void _onError(String message) {
            }

            @Override
            protected void _onCompleted() {
            }
        });
    }

    @Override
    public void uploadBindResult(String cardNo,String strRet) {
        if(App.getConfig().getUserInfo() != null){
            String userId = "";
            String noAgree = "";
            try {
                JSONObject jobj = new JSONObject(strRet);
                userId = jobj.getString("user_id");
                noAgree = jobj.getString("no_agree");
            }catch (Exception e){
                e.printStackTrace();
            }
            toSubscribe(HttpManager.getApi().saveBindResult(cardNo, userId, noAgree), new HttpSubscriber() {
                @Override
                protected void _onStart() {
                    mView.showLoading("");
                }

                @Override
                protected void _onNext(Object o) {
                    ToastUtil.show("银行卡绑定成功");
                    EventBus.getDefault().post(new FragmentRefreshEvent(UIBaseEvent.EVENT_BANK_CARD_SUCCESS));
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorMsg(message,null);
                }

                @Override
                protected void _onCompleted() {
                    mView.stopLoading();
                    mView.finish();
                }
            });

        }
    }
}
